package com.beamfield.controller.manage.process;

import com.alibaba.fastjson.JSON;
import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.Leave;
import com.beamfield.model.ProcPlan;
import com.beamfield.model.Reeducation;
import com.beamfield.modelVo.ProcPlanVo;
import com.beamfield.service.manage.process.ProcessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Zephyr
 * @Description:
 * @Date 2018/8/7
 */
@Controller
@RequestMapping(value = "/cms/process/plan")
public class ProcessPlanController extends BaseController{
    private final ProcessService processService;

    @Autowired
    public ProcessPlanController(ProcessService processService) {
        this.processService = processService;
    }

    @RequestMapping(value = "/plan",method = RequestMethod.GET)
    public ModelAndView plan(){
        return new ModelAndView();
    }

    @RequestMapping(value = "/modifyBeamNo",method = RequestMethod.GET)
    public ModelAndView modifyBeamNo(){
        return new ModelAndView();
    }
    /**
     * 获取生产计划数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/readProcPlanData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readProcPlanData(HttpServletRequest request){
        long beamId = super.getBeamId(request);
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String projectId = request.getParameter("projectId");
        List<ProcPlanVo> procPlanVoList = processService.readProcPlanData(beamId, offset, limit, projectId);
        long total = processService.countProcPlanData(beamId,projectId);
        return new PageResult(total, CommonUtils.parseLong(offset,0L),procPlanVoList);
    }

    /**
     * 删除生产计划
     * @param request
     * @return
     */
    @RequestMapping(value = "/delProcPlan",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult delProcPlan(HttpServletRequest request){
        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            return processService.delProcPlan(id);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }
    /**
     * 保存生产计划
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveProcPlan",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveProcPlan(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String procPlanJson = request.getParameter("procPlanJson");
        if(StringUtils.isEmpty(procPlanJson)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        try{
            ProcPlan procPlan = JSON.parseObject(procPlanJson, ProcPlan.class);
            return processService.saveProcPlan(beamId, userId, procPlan);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }


    /**
     * 变更施工部位
     * @param request
     * @return
     */
    @RequestMapping(value = "/modifyBeamNo",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult modifyBeamNo(HttpServletRequest request){
        String oldProjectDetailId = request.getParameter("oldProjectDetailId");
        String newProjectDetailId = request.getParameter("newProjectDetailId");
        if(StringUtils.isEmpty(oldProjectDetailId) || StringUtils.isEmpty(newProjectDetailId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        int result = 0;
        try{
            result = processService.modifyBeamNo(CommonUtils.parseLong(oldProjectDetailId, 0L), CommonUtils.parseLong(newProjectDetailId, 0L), beamId, userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 0){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }else {
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(), "修改成功");
        }
    }


}
